<template>
	<view>
		<form>
			<view class="releases">
				<view class="list">
					<view class="acea-row row-middle row-between item border-b">
						<view class="label label-m">
							姓名
						</view>
						<view class="item-r">
							<input class="input-r" type="text" placeholder-class="placeholder-class" placeholder="请输入姓名"
								v-model="dataform.username" name='username'>
						</view>
					</view>
					<view class="acea-row row-middle row-between item border-b">
						<view class="label">
							备注
						</view>
						<view class="item-r">
							<input class="input-r" type="text" placeholder-class="placeholder-class" placeholder="设置备注"
								v-model="dataform.remask" name='remark'>
						</view>
					</view>
					<view class="acea-row row-middle row-between item border-b">
						<view class="label label-m">
							电话
						</view>
						<view class="item-r">
							<input class="input-r" type="number" placeholder-class="placeholder-class"
								placeholder="请输入电话" v-model="dataform.phone" name='phone'>
						</view>
					</view>
					<view class="acea-row row-middle row-between item border-b">
						<view class="label label-m">
							岗位
						</view>
						<view class="item-r">
							<input class="input-r" type="text" placeholder-class="placeholder-class"
								placeholder="请输入岗位" v-model="dataform.job" name='job'>
						</view>
					</view>
					<view class="acea-row row-middle row-between item border-b">
						<view class="label label-m">
							地址
						</view>
						<view class="acea-row row-middle row-right item-r address">
							<input type="text" placeholder="请输入/选择公司地址" v-model="dataform.address"
								placeholder-class="placeholder-class">
							<image src="../static/images/location.png" mode="" @click="mapChooseLocation"></image>
						</view>
					</view>
 
				</view>
				<button class="submit-btn" form-type="submit" @click="formSubmit">
					完成
				</button>
			</view>
		</form>
	</view>
</template>

<script>
	import {
		Debounce
	} from '@/utils/validate.js'
	import {
		contactAdd,
		contactUpdate
	} from "@/api/subletOfShop.js"
	import {
		getUserInfo
	} from '@/api/user.js'
	export default {
		data() {
			return {
				username: '',
				remask: '',
				phone: '',
				identityList: ['房东','老板', '雇员'],
				identityId: '',
				dataform: {},
				identity:'',
				infoIndex: null
			};
		},
		onLoad(option) {
			if (!option.info) {
				return
			}else{
				this.autoCurUser()
			}
			this.dataform = JSON.parse(option.info);
			this.infoIndex = option.infoIndex;
			this.identity = this.identityList[this.dataform.type - 1];
			
		},
		methods: {
			identityPickerChange(e) {
				let idx = e.detail.value
				this.identity = this.identityList[idx];
			},
			// formSubmit(e) {
			// 	if (this.identity == '老板') {
			// 		e.detail.value.type = 1
			// 	} else if (this.identity == '房东') {
			// 		e.detail.value.type = 2
			// 	} else {
			// 		e.detail.value.type = 3
			// 	}
			// 	// contactAdd({...e.detail.value}).then(res => {
			// 	// 	console.log(res)
			// 	// })
			// },
			async formSubmit() {
				let that = this;
				if (!/^[\w\d]{5,16}$/i.test(that.dataform.phone)) return that.$util.Tips({
					title: '请输入正确的手机号码'
				});
				if (!that.dataform.username) return that.$util.Tips({
					title: '请填写姓名'
				});
				// if (!that.identity) return that.$util.Tips({
				// 	title: '请选择身份'
				// });
				// if (this.identity == '老板') {
				// 	this.dataform.type = 2
				// } else if (this.identity == '房东') {
				// 	this.dataform.type = 1
				// } else {
				// 	this.dataform.type = 3
				// }
				let aa = {}
				let res = !this.dataform.id ? await contactAdd({...this.dataform}) : await contactUpdate({...this.dataform});
				aa = res.data
				aa.open = 0
				aa.contactId = res.data.id
				!this.dataform.id ? uni.$emit("contact", aa) : uni.$emit("upContact", {info:aa,index:this.infoIndex})
				uni.navigateBack()
			},
			async mapChooseLocation() {
				uni.chooseLocation({
					success: (res) => {
						this.$set(this.dataform, 'address', res.address)
						this.$set(this.dataform, 'latitude', res.latitude)
						this.$set(this.dataform, 'longitude', res.longitude)
					},
				})
			},
			//获取当前用户
			autoCurUser(){
				if(!this.dataform.id){
					getUserInfo().then(res=>{
						console.log(res)
					})
				}
			}
		}

	}
</script>

<style lang="scss">
	// @import url("../../../static/css/releaseForm.css");
	@import '/static/css/releaseForm.css';
</style>